import pika

# RabbitMQ 连接配置
RABBITMQ_CONFIG = {
    "host": "10.0.106.15",
    "port": 5672,
    "username": "root",
    "password": "avs123456.",
}

# 构建连接参数对象
credentials = pika.PlainCredentials(
    username=RABBITMQ_CONFIG["username"], password=RABBITMQ_CONFIG["password"]
)
connection_params = pika.ConnectionParameters(
    host=RABBITMQ_CONFIG["host"], port=RABBITMQ_CONFIG["port"], credentials=credentials
)

# 建立连接
connection = pika.BlockingConnection(connection_params)
channel = connection.channel()

# 声明队列，确保和发布者用的是同一个队列名
queue_name = "test_queue"
channel.queue_declare(queue=queue_name, durable=True)


# 定义回调函数来处理消息
def callback(ch, method, properties, body):
    print(f" [x] Received message: {body.decode('utf-8')}")
    # 手动确认消息
    ch.basic_ack(delivery_tag=method.delivery_tag)


# 设置消费者
channel.basic_consume(
    queue=queue_name,
    on_message_callback=callback,
    auto_ack=False,  # 设置为 False 以便手动确认
)

print(" [*] Waiting for messages. To exit press CTRL+C")
channel.start_consuming()
